package com.ferrlo.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ferrlo.model.Project;
import com.ferrlo.model.User;
import com.ferrlo.service.IProjectManager;

@Controller
public class CategoryController {

	@Resource(name="projectManager")
	private IProjectManager projectManager;
	
	@RequestMapping(value="/category")
	public String toCategory(Map<String, Object> map,HttpServletRequest request){
		
		String category	= request.getParameter("category");
		List<Project> projects = findProjectByCategory(category);
		
		map.put("projects", projects);
		
		return "index";
	}
	
	@RequestMapping(value="/category.json")
	public Map<String, Object> toCategory_JSON(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		
		String category = request.getParameter("category");
		List<Project> projects = findProjectByCategory(category);
		map.put("projects", projects);
		
		return map;
	}
	
	private List<Project> findProjectByCategory(String category){
		
		String dqQuery = "ProjectManager.findUserByUNickName.query";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("category", category);
		List<Project> projects = projectManager.findListByDynamicQuery(dqQuery, param);
		
		return projects;
	}

}
